<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('sales.custbranch.branchcode')" type="string" v-model="baTable.form.items!.branchcode" prop="branchcode" :placeholder="t('Please input field', { field: t('sales.custbranch.branchcode') })" />
                    <FormItem :label="t('sales.custbranch.debtorno')" type="remoteSelect" v-model="baTable.form.items!.debtorno" prop="debtorno" :input-attr="{ pk: 'cncerp_sales_debtorsmaster.debtorno', field: 'name', 'remote-url': '/admin/sales.Debtorsmaster/index' }" :placeholder="t('Please select field', { field: t('sales.custbranch.debtorno') })" />
                    <FormItem :label="t('sales.custbranch.brname')" type="string" v-model="baTable.form.items!.brname" prop="brname" :placeholder="t('Please input field', { field: t('sales.custbranch.brname') })" />
                    <FormItem :label="t('sales.custbranch.braddress1')" type="string" v-model="baTable.form.items!.braddress1" prop="braddress1" :placeholder="t('Please input field', { field: t('sales.custbranch.braddress1') })" />
                    <FormItem :label="t('sales.custbranch.braddress2')" type="string" v-model="baTable.form.items!.braddress2" prop="braddress2" :placeholder="t('Please input field', { field: t('sales.custbranch.braddress2') })" />
                    <FormItem :label="t('sales.custbranch.braddress3')" type="string" v-model="baTable.form.items!.braddress3" prop="braddress3" :placeholder="t('Please input field', { field: t('sales.custbranch.braddress3') })" />
                    <FormItem :label="t('sales.custbranch.braddress4')" type="string" v-model="baTable.form.items!.braddress4" prop="braddress4" :placeholder="t('Please input field', { field: t('sales.custbranch.braddress4') })" />
                    <FormItem :label="t('sales.custbranch.braddress5')" type="string" v-model="baTable.form.items!.braddress5" prop="braddress5" :placeholder="t('Please input field', { field: t('sales.custbranch.braddress5') })" />
                    <FormItem :label="t('sales.custbranch.braddress6')" type="string" v-model="baTable.form.items!.braddress6" prop="braddress6" :placeholder="t('Please input field', { field: t('sales.custbranch.braddress6') })" />
                    <FormItem :label="t('sales.custbranch.lat')" type="number" prop="lat" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.lat" :placeholder="t('Please input field', { field: t('sales.custbranch.lat') })" />
                    <FormItem :label="t('sales.custbranch.lng')" type="number" prop="lng" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.lng" :placeholder="t('Please input field', { field: t('sales.custbranch.lng') })" />
                    <FormItem :label="t('sales.custbranch.estdeliverydays')" type="number" prop="estdeliverydays" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.estdeliverydays" :placeholder="t('Please input field', { field: t('sales.custbranch.estdeliverydays') })" />
                    <FormItem :label="t('sales.custbranch.area')" type="remoteSelect" v-model="baTable.form.items!.area" prop="area" :input-attr="{ pk: 'cncerp_sales_areas.areacode', field: 'areadescription', 'remote-url': '/admin/sales.Areas/index' }" :placeholder="t('Please select field', { field: t('sales.custbranch.area') })" />
                    <FormItem :label="t('sales.custbranch.salesman')" type="remoteSelect" v-model="baTable.form.items!.salesman" prop="salesman" :input-attr="{ pk: 'cncerp_sales_salesman.salesmancode', field: 'salesmanname', 'remote-url': '/admin/sales.Salesman/index' }" :placeholder="t('Please select field', { field: t('sales.custbranch.salesman') })" />
                    <FormItem :label="t('sales.custbranch.fwddate')" type="number" prop="fwddate" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.fwddate" :placeholder="t('Please input field', { field: t('sales.custbranch.fwddate') })" />
                    <FormItem :label="t('sales.custbranch.phoneno')" type="string" v-model="baTable.form.items!.phoneno" prop="phoneno" :placeholder="t('Please input field', { field: t('sales.custbranch.phoneno') })" />
                    <FormItem :label="t('sales.custbranch.faxno')" type="string" v-model="baTable.form.items!.faxno" prop="faxno" :placeholder="t('Please input field', { field: t('sales.custbranch.faxno') })" />
                    <FormItem :label="t('sales.custbranch.contactname')" type="string" v-model="baTable.form.items!.contactname" prop="contactname" :placeholder="t('Please input field', { field: t('sales.custbranch.contactname') })" />
                    <FormItem :label="t('sales.custbranch.email')" type="string" v-model="baTable.form.items!.email" prop="email" :placeholder="t('Please input field', { field: t('sales.custbranch.email') })" />
                    <FormItem :label="t('sales.custbranch.defaultlocation')" type="remoteSelect" v-model="baTable.form.items!.defaultlocation" prop="defaultlocation" :input-attr="{ pk: 'cncerp_stock_locations.loccode', field: 'locationname', 'remote-url': '/admin/stock.Locations/index' }" :placeholder="t('Please select field', { field: t('sales.custbranch.defaultlocation') })" />
                    <FormItem :label="t('sales.custbranch.taxgroupid')" type="remoteSelect" v-model="baTable.form.items!.taxgroupid" prop="taxgroupid" :input-attr="{ pk: 'cncerp_setup_taxgroups.taxgroupid', field: 'taxgroupdescription', 'remote-url': '/admin/setup.Taxgroups/index' }" :placeholder="t('Please select field', { field: t('sales.custbranch.taxgroupid') })" />
                    <FormItem :label="t('sales.custbranch.defaultshipvia')" type="remoteSelect" v-model="baTable.form.items!.defaultshipvia" prop="defaultshipvia" :input-attr="{ pk: 'cncerp_sales_shippers.shipper_id', field: 'shippername', 'remote-url': '/admin/sales.Shippers/index' }" :placeholder="t('Please select field', { field: t('sales.custbranch.defaultshipvia') })" />
                    <FormItem :label="t('sales.custbranch.deliverblind')" type="radio" v-model="baTable.form.items!.deliverblind" prop="deliverblind" :data="{ content: { '1': t('sales.custbranch.deliverblind 1'), '2': t('sales.custbranch.deliverblind 2') } }" :placeholder="t('Please select field', { field: t('sales.custbranch.deliverblind') })" />
                    <FormItem :label="t('sales.custbranch.disabletrans')" type="switch" v-model="baTable.form.items!.disabletrans" prop="disabletrans" :data="{ content: { '0': t('sales.custbranch.disabletrans 0'), '1': t('sales.custbranch.disabletrans 1') } }" />
                    <FormItem :label="t('sales.custbranch.brpostaddr1')" type="string" v-model="baTable.form.items!.brpostaddr1" prop="brpostaddr1" :placeholder="t('Please input field', { field: t('sales.custbranch.brpostaddr1') })" />
                    <FormItem :label="t('sales.custbranch.brpostaddr2')" type="string" v-model="baTable.form.items!.brpostaddr2" prop="brpostaddr2" :placeholder="t('Please input field', { field: t('sales.custbranch.brpostaddr2') })" />
                    <FormItem :label="t('sales.custbranch.brpostaddr3')" type="string" v-model="baTable.form.items!.brpostaddr3" prop="brpostaddr3" :placeholder="t('Please input field', { field: t('sales.custbranch.brpostaddr3') })" />
                    <FormItem :label="t('sales.custbranch.brpostaddr4')" type="string" v-model="baTable.form.items!.brpostaddr4" prop="brpostaddr4" :placeholder="t('Please input field', { field: t('sales.custbranch.brpostaddr4') })" />
                    <FormItem :label="t('sales.custbranch.brpostaddr5')" type="string" v-model="baTable.form.items!.brpostaddr5" prop="brpostaddr5" :placeholder="t('Please input field', { field: t('sales.custbranch.brpostaddr5') })" />
                    <FormItem :label="t('sales.custbranch.brpostaddr6')" type="string" v-model="baTable.form.items!.brpostaddr6" prop="brpostaddr6" :placeholder="t('Please input field', { field: t('sales.custbranch.brpostaddr6') })" />
                    <FormItem :label="t('sales.custbranch.specialinstructions')" type="textarea" v-model="baTable.form.items!.specialinstructions" prop="specialinstructions" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('sales.custbranch.specialinstructions') })" />
                    <FormItem :label="t('sales.custbranch.custbranchcode')" type="string" v-model="baTable.form.items!.custbranchcode" prop="custbranchcode" :placeholder="t('Please input field', { field: t('sales.custbranch.custbranchcode') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    branchcode: [buildValidatorData({ name: 'required', title: t('sales.custbranch.branchcode') })],
    debtorno: [buildValidatorData({ name: 'required', title: t('sales.custbranch.debtorno') })],
    brname: [buildValidatorData({ name: 'required', title: t('sales.custbranch.brname') })],
    lat: [buildValidatorData({ name: 'number', title: t('sales.custbranch.lat') })],
    lng: [buildValidatorData({ name: 'number', title: t('sales.custbranch.lng') })],
    estdeliverydays: [buildValidatorData({ name: 'number', title: t('sales.custbranch.estdeliverydays') })],
    area: [buildValidatorData({ name: 'required', title: t('sales.custbranch.area') })],
    salesman: [buildValidatorData({ name: 'required', title: t('sales.custbranch.salesman') })],
    fwddate: [buildValidatorData({ name: 'number', title: t('sales.custbranch.fwddate') })],
    phoneno: [buildValidatorData({ name: 'mobile', title: t('sales.custbranch.phoneno') })],
    email: [buildValidatorData({ name: 'email', title: t('sales.custbranch.email') })],
    defaultlocation: [buildValidatorData({ name: 'required', title: t('sales.custbranch.defaultlocation') })],
    taxgroupid: [buildValidatorData({ name: 'required', title: t('sales.custbranch.taxgroupid') })],
    defaultshipvia: [buildValidatorData({ name: 'required', title: t('sales.custbranch.defaultshipvia') })],
    deliverblind: [buildValidatorData({ name: 'required', title: t('sales.custbranch.deliverblind') })],
})
</script>

<style scoped lang="scss"></style>
